package cn.sunan.org.controller;

import cn.sunan.basic.uitl.Ajaxresult;
import cn.sunan.basic.uitl.PageList;
import cn.sunan.org.domain.Department;
import cn.sunan.org.query.DepartmentQuery;
import cn.sunan.org.service.IDepartmentService;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dete")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @GetMapping
    public List<Department> quertAll(){
        return departmentService.selectAll();
    }
    @GetMapping("/{id}")
    public  Department selectById(@PathVariable("id") Long id){
        return departmentService.selctById(id);
    }
    @PostMapping
    public PageList<Department> selectquery(@RequestBody DepartmentQuery department){
        System.out.println(department.getStart());
        System.out.println(department.getCurrentPage());
       return   departmentService.queryByPage(department);
    }
    @PutMapping
    public  Ajaxresult saveOrupdate(@RequestBody Department department) {
        try {
            if (department.getId()==null)
                departmentService.save(department);
            else {
                departmentService.update(department);
            }
            return  new Ajaxresult();
        } catch (Exception e) {
            e.printStackTrace();
            return new Ajaxresult(false,"操作失败");
        }

    }
    @DeleteMapping("/{id}")
    public Ajaxresult delete(@PathVariable("id") Long id){
         try {
             departmentService.delete(id);
             return new Ajaxresult();
         }catch (Exception e){
            e.printStackTrace();
            return  new Ajaxresult(false,"操作失败");
         }

    }
    @PatchMapping
    public Ajaxresult delete1(@RequestBody List<Long> ids){
        try {
                departmentService.batchDetele(ids);
                return new Ajaxresult();
        }catch (Exception e){
            e.printStackTrace();
            return  new Ajaxresult(false,"删除失败");
        }
    }
    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }
}
